package com.gitee.zycra.future.job.service;

import com.gitee.zycra.future.job.common.bo.CommonResult;
import com.gitee.zycra.future.job.common.bo.PageQuery;
import com.gitee.zycra.future.job.common.param.AddMetaDataParam;
import com.gitee.zycra.future.job.common.param.PageQueryMetaDataParam;
import com.gitee.zycra.future.job.common.param.UpdateMetaDataParam;
import com.gitee.zycra.future.job.common.vo.JobMetaDataVO;
import com.gitee.zycra.future.job.manager.JobMetaDataManager;
import com.gitee.zycra.future.job.manager.JobScheduleManager;
import com.gitee.zycra.jdbc.model.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zycra
 */
@Service
public class JobMetaDataService {

    @Autowired
    private JobMetaDataManager jobMetaDataManager;

    @Autowired
    private JobScheduleManager jobScheduleManager;

    /**
     * 插入
     *
     * @param addMetaDataParam 元数据对象
     * @return 插入条数
     */
    public int insertJobMetaData(AddMetaDataParam addMetaDataParam) {
        return jobMetaDataManager.insertJobMetaData(AddMetaDataParam.buildJobMetaData(addMetaDataParam));
    }

    /**
     * 删除
     *
     * @param id 要删除的ID
     * @return 删除条数
     */
    public CommonResult<String> deleteJobMetaData(Long id) {
        Integer scheduleCount = jobScheduleManager.countJobScheduleByJobMetaDataId(id);
        if (scheduleCount > 0) {
            return CommonResult.failed("存在调度配置, 无法删除元数据");
        }
        jobMetaDataManager.deleteJobMetaData(id);
        return CommonResult.success();
    }

    /**
     * 更新
     *
     * @param updateMetaDataParam 元数据对象
     * @return 更新条数
     */
    public int updateJobMetaData(UpdateMetaDataParam updateMetaDataParam) {
        return jobMetaDataManager.updateJobMetaData(UpdateMetaDataParam.buildJobMetaData(updateMetaDataParam));
    }

    /**
     * 分页查询
     *
     * @param pageQuery 查询参数
     * @return 查询到的数据
     */
    public PageResult<JobMetaDataVO> selectJobMetaDataForPage(PageQuery<PageQueryMetaDataParam> pageQuery) {
        return jobMetaDataManager.selectJobMetaDataForPage(pageQuery);
    }
}
